
import { searchAffairByBusApi } from './service/pageAjax'
const iconClose=require('./assets/close.png')
class iMeta {
    constructor({ el, vm }) {
        this.el = this.isElementNode(el) ? el : document.querySelector(el).parentNode
      
        this.vm = vm;
        if (this.el) {
            this.onLoad()
        }
    }

    //****************工具方法 ************
    isElementNode(node) {
        return node.nodeType === 1
    }
    isTextNode(node) {
        return node.nodeType === 3
    }
    addView(){
        const div = document.createElement('div')
        div.className='affair'
        let lable = document.createElement('lable')
        lable.innerHTML='关联事务'
        lable.className='affair-btn'
        lable.onclick=() => {
            document.getElementById('affair-drag').style.display="block"
        }
        div.appendChild(lable);

        this.el.appendChild(div);
    }
    async onLoad() {
        const that = this
        that.addView()
        // var img=new Image();  
        // img.src=ss
        // this.el.appendChild(img); 
        // return
        const res = await this.initData(),
            { data, code } = res
        if (code === 200) {

            let dl = document.createElement('dl')
            dl.className = 'affair-drag'
            dl.id='affair-drag'
            const dt = document.createElement('dt')
            dt.innerHTML = '相关事务'
            const close = document.createElement('img')
            close.src=iconClose
            close.onclick=() => {
                document.getElementById('affair-drag').style.display="none"
            }
            dt.appendChild(close)
            const ddtile = document.createElement('dd')
            ddtile.className='dd-tile'
            const liA = document.createElement('li')
            liA.innerHTML = '人员'
            const liB = document.createElement('li')
            liB.innerHTML = '标题'
            const liC = document.createElement('li')
            liC.innerHTML = '内容'
            const liD = document.createElement('li')
            liD.innerHTML = '图片'
            const liE = document.createElement('li')
            liE.innerHTML = '时间'
            ddtile.appendChild(liA)
            ddtile.appendChild(liB)
            ddtile.appendChild(liC)
            ddtile.appendChild(liD)
            ddtile.appendChild(liE)

            // content
         

            const dd = document.createElement('dd')
            dd.className='dd-main'
            data.forEach(element => {
                let ul = document.createElement('ul')

                const li2A = document.createElement('li')
                li2A.innerHTML = element.creatorName
                const li2B = document.createElement('li')
                li2B.innerHTML = element.sn ? element.sn + '-' : ''
                li2B.innerHTML +=  element.affairName 
               
                const li2C = document.createElement('li')
                li2C.innerHTML = element.content

                const li2D = document.createElement('li')
                let imgList = []
                if(element.image.includes(',')) {
                    imgList = element.image.split(',') 
                }
                imgList.forEach(e => {
                    const img = document.createElement('img')
                    img.src=e
                    li2D.append(img)
                })
                const li2E = document.createElement('li')
                li2E.innerHTML = that.period(element.startAt,element.endAt)




                ul.appendChild(li2A)
                ul.appendChild(li2B)
                ul.appendChild(li2C)
                ul.appendChild(li2D)
                ul.appendChild(li2E)


                dd.appendChild(ul);
            });
            if(data.length === 0) {
                let nodata = document.createElement('font')
                nodata.innerHTML = '暂无相关事务'
                dd.appendChild(nodata)
            }
            dl.appendChild(dt);
            dl.appendChild(ddtile);

            dl.appendChild(dd);
            that.el.appendChild(dl); 
        }
    }
    async initData() {
        const params = {
            "busId": this.getQueryVariable('id'),
            "current": 1,
            "endTime": "",
            "id": "",
            "isBind": "",
            "keyword": "",
            "monthTime": "",
            "project": "",
            "size": 1000,
            "startTime": ""
        }
        return await searchAffairByBusApi(params)
    }
    period(startAt,endAt){
        const a = startAt ? startAt : ''
        const b = endAt ? endAt : ''
        const c= startAt && endAt ? ' <br/> ' : ''
        return a + c + b

    }
    getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) { return pair[1]; }
        }
        return (false);
    }
}
export default iMeta